from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import schedule
from datetime import datetime, timedelta, time
import time
import threading
from selenium.common.exceptions import TimeoutException, NoSuchElementException, WebDriverException


def job(user, password):
    s = Service("chromedriver.exe")
    browser = webdriver.Chrome(service=s)
    url = 'https://static.sdjzu.edu.cn/h5/#/pages/user/login?url=%252Fpages%252Fapplication%252Furgent-report%252Ftemperature-measurement-report'
    # url = 'https://static.sdjzu.edu.cn/h5/#/pages/application/urgent-report/temperature-measurement-report'
    #
    browser.get(url=url)
    # 这个是以秒为计时单位
    time.sleep(13)

    # 获取文本框的对象
    input1 = browser.find_element(by=By.XPATH,
                                  value='/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[1]/uni-input/div/input').send_keys(
        user)
    input2 = browser.find_element(by=By.XPATH,
                                  value='/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view[2]/uni-input/div/input').send_keys(
        password)
    time.sleep(14)

    button = browser.find_element(by=By.XPATH,
                                  value='/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-button')

    button.click()

    time.sleep(14)

    base_url = '/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-view/uni-view[{}]/uni-view/uni-view[2]/uni-radio-group/uni-label[1]/uni-radio/div/div'
    for i in range(1, 7):
        url = base_url.format(i)
        radio = browser.find_element(by=By.XPATH,
                                     value=url)
        radio.click()
        time.sleep(1)

    # 获取到我们的button
    button2 = browser.find_element(by=By.XPATH,
                                   value='/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[3]/uni-button')

    button2.click()
    time.sleep(2)


def job1():
    user = '202111110121'
    password = 'yT1615767372'
    job(user, password)


def job2():
    user = '202111110122'
    password = 'SDjzu11087154'
    job(user, password)


def job3():
    print("I'm running on thread %s" % threading.current_thread())


def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()


schedule.every().day.at("17:14").do(run_threaded, job1)
schedule.every().day.at("17:14").do(run_threaded, job2)
while True:
    try:
        schedule.run_pending()
        # print('等待中')
    except (NoSuchElementException, WebDriverException):
        print('')
